package cn.niit.controller;

import cn.niit.entity.Course;
import cn.niit.entity.Teacher;
import cn.niit.entity.query.CourseQuery;
import cn.niit.entity.query.TeacherQuery;
import cn.niit.service.CourseService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Controller
@RequestMapping("/course")
public class CourseController {
    @Resource
    private CourseService courseService;
    @GetMapping("/query")
    public String queryByCourse(Model model, CourseQuery courseQuery) {
        PageInfo<Course> coursePageInfo = courseService.queryByCourse(courseQuery);
        model.addAttribute("page", coursePageInfo);
        return "/course/course";
    }
    @GetMapping("/select")
    public String select(Model model,CourseQuery courseQuery){
        PageInfo<Course> coursePageInfo = courseService.select(courseQuery);
        model.addAttribute("page", coursePageInfo);
        return "/course/course";
    }

    @GetMapping("/edit/{id}")
    public String toEdit(@PathVariable Integer id, Model model) {
        model.addAttribute("course", courseService.queryCourseById(id));
        return "/course/editCourse";
    }

    @PostMapping("/edit")
    public String edit(Course course) {
        CourseQuery courseQuery = new CourseQuery();
        Integer id = course.getId();
        courseQuery.setCourseName(course.getCourseName());
        PageInfo<Course> coursePageInfo = courseService.queryByCourseName(courseQuery);
        boolean b;
        if (id != null) {
            b = courseService.editCourse(course);
        } else {
            b = courseService.addCourse(course);
        }
        return "redirect:/course/query";
    }

    @GetMapping("/update")
    public String update(Model model) {
            Course course=new Course();
        model.addAttribute("course", course);
        return "/course/editCourse";
    }

    @GetMapping("/delete/{id}")
    public String deleteCourseById(@PathVariable("id") Integer id) {
        boolean b = courseService.deleteCourseById(id);
        return "redirect:/course/query";
    }
}